package com.oc.mba.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.oc.mba.dao.BaseDAOHibernate;
import com.oc.mba.dao.OcQuestionOptionDAO;
import com.oc.mba.entity.OcQuestion;
import com.oc.mba.entity.OcQuestionOption;
import com.oc.mba.util.CommLog;

@Repository
public class OcQuestionOptionDAOImpl extends BaseDAOHibernate implements OcQuestionOptionDAO {
	private static final CommLog logger = new CommLog(OcQuestionOptionDAOImpl.class);

	public List<OcQuestionOption> findByProperty(String propertyName, Object value) {
		logger.debug("finding OcQuestionOption instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from OcQuestionOption as model where model."
					+ propertyName + "= ? order by model.id";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			logger.error("find by property name failed", re);
			throw re;
		}
	}
	
	@Override
	public void save(OcQuestionOption option) {
		getHibernateTemplate().save(option);
	}
	
	@Override
	public OcQuestionOption findQuestionOption(Integer optionID) {
		List<OcQuestionOption> optionList = findByProperty("id", optionID);
		if (optionList != null && optionList.size() > 0) {
			return optionList.get(0);
		}
		return null;
	}

	@Override
	public void deleteOption(Integer optionID) {
		OcQuestionOption option = findQuestionOption(optionID);
		if (option != null) {
			getHibernateTemplate().delete(option);
		}
	}

	@Override
	public void updateQuestionOptionFault(Integer optionID) {
		OcQuestionOption option = findQuestionOption(optionID);
		if (option != null) {
			option.setOptionCorrect((short)0);
			getHibernateTemplate().update(option);
		}
	}

	@Override
	public void updateQuestionOptionCorrect(Integer optionID) {
		OcQuestionOption option = findQuestionOption(optionID);
		if (option != null) {
			option.setOptionCorrect((short)1);
			getHibernateTemplate().update(option);
		}
	}
	
}
